package structures.review.queue;

import com.sun.istack.internal.NotNull;

import java.util.Arrays;
import java.util.Scanner;

// 先入先出
public class ArrayQueueDemo {
    public static void main(String[] args) {

        ArrayQueue<String> arrayQueue = new ArrayQueue<>();

        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println();
            System.out.println("s(show) 显示队列全部数据");
            System.out.println("e(exit) 退出程序");
            System.out.println("a(add) 添加数据");
            System.out.println("g(get) 获取数据");
            System.out.println("r(remove) 删除数据");
            System.out.println("h(head) 查看头部数据");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.list();
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                case 'a':
                    arrayQueue.addData(scanner.next());
                    break;
                case 'r':
                    System.out.println(arrayQueue.remove(scanner.nextInt()));
                    break;
                case 'g':
                    System.out.println(arrayQueue.getData(scanner.nextInt()));
                    break;
                case 'h':
//                    System.out.println(arrayQueue.getHeadQueue());
                    break;
            }
            System.out.println("------------------------");
        }
    }
}

class ArrayQueue<T> {
    transient Object[] dataArray; // 保存数据的数据\
    private int rear;   // 指向最后一个数据
    private static final int DEFAULT_SIZE = 5;

    public ArrayQueue() {
        this(DEFAULT_SIZE);
    }

    public ArrayQueue(int maxSize) {
        dataArray = new Object[maxSize];
    }

    // 获取数据
    @SuppressWarnings("unchecked")
    public T getData(@NotNull int index) {
        if (index >= rear) {
            throw new RuntimeException("没有数据可以获取");
        }
        return (T) dataArray[index];
    }

    // remove数据
    @SuppressWarnings("unchecked")
    public T remove(int index) {
        if (index >= rear) {
            throw new RuntimeException("没有数据可以删除  越界了");
        }
        Object data = dataArray[index];
        System.arraycopy(dataArray, index + 1, dataArray, index, rear - index - 1);
        dataArray[--rear] = null;
        return (T) data;
    }

    // 添加数据
    public void addData(T data) {
        if (rear >= dataArray.length) {
            resize(rear);
        }
        dataArray[rear++] = data;
    }

    public int size() {
        return rear;
    }

    public boolean isEmpty() {
        return rear == 0;
    }

    // 扩展数组
    private void resize(int rear) {
        int length = rear + (rear >> 1);
        dataArray = Arrays.copyOf(dataArray, length);
        System.out.println(dataArray.length);
    }

    // 遍历数据
    @SuppressWarnings("unchecked")
    public void list() {
        for (Object data : dataArray) {
            System.out.println((T) data);
        }
    }
}